package com.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.PaySystemConstant;
import com.example.config.exception.GlobalExcetion;
import com.example.mapper.UserPayRecordInMapper;
import com.example.pay.domain.UserPayRecordIn;

import com.example.service.UserPayRecordInService;
import com.example.system.domain.BusSystemTableUser;
import com.example.system.service.BusSystemTableService;
import com.example.system.service.BusSystemTableUserService;
import com.example.util.sql.DBSql;
import com.example.util.sql.SqlGenerator;
import com.example.util.sql.SqlResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Date;

/**
* @author CETC
* @description 针对表【user_pay_record_in_1】的数据库操作Service实现
* @createDate 2025-11-01 22:17:24
*/
@Service
public class UserPayRecordInServiceImpl extends ServiceImpl<UserPayRecordInMapper, UserPayRecordIn>
    implements UserPayRecordInService {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private BusSystemTableService busSystemTableService;

    @Autowired
    private BusSystemTableUserService busSystemTableUserService;

    @Override
    public UserPayRecordIn saveUserPayRecordIn(String userId, UserPayRecordIn userPayRecordIn) throws GlobalExcetion {
        if(userPayRecordIn==null){
            throw new GlobalExcetion(1,"传入数据不得为空");
        }
        BusSystemTableUser busSystemTableUser =busSystemTableUserService.selectByUserId(userId, PaySystemConstant.User_Pay_RecordIn_tableName);
        String tableName = busSystemTableUser.getSaveTableName();
        String uuid=busSystemTableService.createUUID(tableName);
        userPayRecordIn.setUuid(uuid);
        userPayRecordIn.setCreateDate(new Date());
        userPayRecordIn.setPayDate(new Date());
        DBSql dbSql = new DBSql(dataSource);
        SqlResult sqlResult= SqlGenerator.generateInsert(tableName, userPayRecordIn);
        dbSql.executeInsert(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userPayRecordIn;
    }

    @Override
    public UserPayRecordIn updateUserPayRecordInById(UserPayRecordIn userPayRecordIn) throws GlobalExcetion {
        if(userPayRecordIn==null || userPayRecordIn.getUuid()==null){
            throw new GlobalExcetion(1,"传入数据不得为空");
        }
        String tableName=busSystemTableService.getTableNameByUUID(userPayRecordIn.getUuid());
        DBSql dbSql = new DBSql(dataSource);
        SqlResult result=SqlGenerator.generateUpdateById(tableName,userPayRecordIn,"uuid");
        dbSql.executeUpdate(result.getSql(),result.getParams().toArray());
        return userPayRecordIn;
    }

    @Override
    public IPage<UserPayRecordIn> page(Page<UserPayRecordIn> page, String userId) throws GlobalExcetion {
        BusSystemTableUser busSystemTableUser =busSystemTableUserService.selectByUserId(userId, PaySystemConstant.User_Pay_RecordIn_tableName);
        String tableName= busSystemTableUser.generateUnionAllTable("to_uuid");
        System.out.println(tableName);
        return this.baseMapper.page(page,tableName,userId);
    }

    @Override
    public UserPayRecordIn addRecord(String fromUUid, String toUUid, String money, String payNote) {
        return null;
    }
}




